﻿using System;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace NewTech_BookStore
{
    public partial class Report : Form
    {
        readonly ReportDataSource _reportDataSource = new ReportDataSource();

        public Report()
        {
            InitializeComponent();

        }




        private void RadCheckedCheck2(object sender, EventArgs e)
        {
            pnlDateTime.Enabled = radMonthPayment.Checked;
            txtDay.Enabled = radDayPayment.Checked;
        }


        private void ReportLoad(object sender, EventArgs e)
        {
            _reportDataSource.Name = "ReportDataSet";
            cbxMonth.SelectedIndex = 0;
            cbxYear.SelectedIndex = 0;
            txtDay.Text = DateTime.Today.ToString("dd/MM/yyyy");
        }

        private void BtnReportClick(object sender, EventArgs e)
        {
            if (radReportBook.Checked)
            {
                BookTableAdapter.Fill(NewTech_BookStoreDataSet.Book);
                _reportDataSource.Value = BookBindingSource;
                reportViewer.LocalReport.ReportEmbeddedResource = "NewTech_BookStore.ReportBookCategory.rdlc";
            }
            else if (radTodayPayment.Checked)
            {
                v_paymentTableAdapter.FillByDay(NewTech_BookStoreDataSet.v_payment, DateTime.Today);
                _reportDataSource.Value = vpaymentBindingSource;
                reportViewer.LocalReport.ReportEmbeddedResource = "NewTech_BookStore.ReportPayment.rdlc";
            }
            else if (radMonthPayment.Checked)
            {
                try
                {
                    var begindate = new DateTime(Convert.ToInt32(cbxYear.Text), Convert.ToInt32(cbxMonth.Text), 1);
                    var enddate = new DateTime();

                    if (begindate.Month > 11)
                        enddate = new DateTime(begindate.Year + 1, 1, 1);
                    else
                        enddate = new DateTime(begindate.Year, begindate.Month + 1, 1);

                    v_paymentTableAdapter.FillByPeriod(NewTech_BookStoreDataSet.v_payment, begindate, enddate);
                    _reportDataSource.Value = vpaymentBindingSource;
                    reportViewer.LocalReport.ReportEmbeddedResource = "NewTech_BookStore.ReportPayment.rdlc";

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            else if (radDayPayment.Checked)
            {
                var day = new DateTime();
                try
                {
                    day = DateTime.ParseExact(txtDay.Text, "dd/MM/yyyy", null);
                    v_paymentTableAdapter.FillByDay(NewTech_BookStoreDataSet.v_payment, day);
                    _reportDataSource.Value = vpaymentBindingSource;
                    reportViewer.LocalReport.ReportEmbeddedResource = "NewTech_BookStore.ReportPayment.rdlc";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            reportViewer.LocalReport.DataSources.Add(_reportDataSource);
            reportViewer.RefreshReport();
        }

    }
}
